﻿<%@ WebHandler Language="C#" Class="Login" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Collections;

public class Login : IHttpHandler , IRequiresSessionState {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        string new_session_id = context.Session.SessionID;
        string new_sip_number = context.Request.Params["SIP_NUMBER"];
        string pps_code = context.Request.Params["PPS_CODE"];
        string custom_name = context.Request.Params["CUSTOM_NAME"];
        string user_dir = context.Request.Params["USER_DIR"];
        string new_time_stamp = context.Timestamp.ToString();
        context.Session["SIP_NUMBER"] = new_sip_number;
        
        //檢查此Session是否已存在Application中
        //讀取Application物件
        context.Application.Lock();
        ArrayList live_session = (ArrayList)context.Application.Get("LIVE_SESSION");
        ArrayList sip_number = (ArrayList)context.Application.Get("SIP_NUMBER");
        ArrayList time_stamp = (ArrayList)context.Application.Get("TIME_STAMP");   

        //重複登入時，session id已在application中
        if (live_session.Contains(new_session_id))
        {
            //找出index,替換application中的sip_number
            int index = live_session.IndexOf(new_session_id);
            sip_number[index] = new_sip_number;
            
            //更新application中的time_stamp
            time_stamp[index] = new_time_stamp;
        }
        else
        {
            //跨瀏覽器重複登入, session_id不在application中
            //但sip_number已在Application中
            if (sip_number.Contains(new_sip_number))
            {
                //找出index, 替換application中的session_id
                int index = sip_number.IndexOf(new_sip_number);
                live_session[index] = new_session_id;

                //更新application中的time_stamp
                time_stamp[index] = new_time_stamp;
            }
            else
            {
                //正常登入，session id 不在application中
                //加入session id和sip number至application中
                live_session.Add(new_session_id);
                sip_number.Add(new_sip_number);

                //加入time_stamp
                time_stamp.Add(new_time_stamp);
            }
        }
                
        context.Application.Set("LIVE_SESSION", live_session);
        context.Application.Set("SIP_NUMBER", sip_number);
        context.Application.Set("TIME_STAMP", time_stamp);
        context.Application.UnLock();        
        context.Response.Write("ok," + new_sip_number + ',' + new_time_stamp + ',' + custom_name + ',' + user_dir);
        
        //Send Message To iSender Robot
        FormUpload.RobotTextSend(new_sip_number, CF.IsenderLoginSuccessedMessage);

        //Create a login log
        CF.WriteLoginLog(new_sip_number);
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }
}